package cn.edu.henu.team.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.bean.clinic;
import cn.edu.henu.team.bean.clinic;

public class VoteAdapter extends BaseAdapter {
    private List<clinic> list;
    private Context context;

    public VoteAdapter(Context context, List<clinic> list) {
        this.context = context;
        this.list = list;
    }

    public void addItem(List<clinic> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null)
            return 0;
        else
            return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_vote, null);
            holder = new ViewHolder();
            holder.name = view.findViewById(R.id.name);
            holder.ratingBar = view.findViewById(R.id.ratingBar);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.name.setText(list.get(i).getName());
        holder.ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                ratingBar.setIsIndicator(true);
                final clinic c = new clinic();
                c.setObjectId(list.get(i).getObjectId());
                c.setName(list.get(i).getName());
                c.setPhone(list.get(i).getPhone());
                c.setAddress(list.get(i).getAddress());
                c.setKinds(list.get(i).getKinds());
                c.setCitys(list.get(i).getCitys());
                c.increment("sum",1);
                c.increment("score",rating * 20);
                c.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(context, "点评成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.d("++++++++++",e.toString());
                            Toast.makeText(context, "点评失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
        return view;
    }

    private class ViewHolder {
        private TextView name;
        private RatingBar ratingBar;
    }
}
